/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:ces.org.course.listener</p>
 * <p>File:FileListener.java</p>
 * <p>类更新历史信息</p>
 * @todo chanlong(陈龙) 创建于 May 17, 2019 3:54:37 PM
 */
package ces.org.course.api.watch;

import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.springframework.stereotype.Component;
import ces.org.course.api.entity.DictionaryEntity;
import ces.org.course.api.service.DictionaryService;
import cn.hutool.core.io.FileUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date May 17, 2019 3:54:37 PM
 * @version 1.0.2019
 */
@Builder @AllArgsConstructor  @Component 
public class FileListener extends FileAlterationListenerAdaptor {
	
	@Override // 文件创建时触发
	public void onFileCreate(final File file) {
		service.put(DictionaryEntity.CACHE_KEY, file);
		System.out.println("文件创建...");
	}

	@Override // 文件删除时触发
	public void onFileDelete(final File file) {
		service.clear(DictionaryEntity.CACHE_KEY);
		FileUtil.loopFiles(file.getParentFile()).forEach(f -> {
			if (null != f && "xml".equals(FileUtil.extName(f))) service.put(DictionaryEntity.CACHE_KEY, f);	
		});
		System.out.println("文件删除...");
	}

	@Override // 文件修改时触发
	public void onFileChange(final File file) {
		service.put(DictionaryEntity.CACHE_KEY, file);
		System.out.println("文件修改...");
	}
	
	private DictionaryService service;
	
}
